
#ifndef _CONST_DEFINE_H_
#define _CONST_DEFINE_H_

typedef unsigned int SOCKET;
typedef unsigned char byte;

#define SUCCESS  (0)
#define FAIL    (-1)

#define TRUE     (1)
#define FALSE    (0)

#define THREAD_STOP     0
#define THREAD_RUN      1
#define THREAD_PAUSE    2

#define INVALID_SOCKET (0xFFFFFFFF)
#define INVALID_CHANNEL_ID (0xFF)

#define SERVER_PORT         3671
#define CLIENT_PORT         (SERVER_PORT+1)
#define CLIENT_DATA_PORT    (CLIENT_PORT+1)
#define TEST_PORT           (CLIENT_DATA_PORT+1)
#define TEST_DATA_PORT      (TEST_PORT+1)

#define MAX_RECV_MSG_LEN        255
#define MAX_DATA_BYTES_LEN      14
#define EIB_DEVICE_ADDRESS_LEN  2

#define MAX_RECV_WAIT_TIME      50
#define HEART_BEAT_INTERVAL     40

#define SERVER_RESPONSE_TIMEOUT  1
#define THREAD_RECV_MSG_TIMEOUT 10

#define KNX_HEART_BEAT_INTERVAL 60
#define KNX_HEART_BEAT_TIMES    3

#define U16_BYTE_HI(port)   ((unsigned char)((port>>8)&0xff))
#define U16_BYTE_LOW(port)   ((unsigned char)(port&0xff))



//KNX define
#define E_NO_ERROR              0x0
#define E_NO_MORE_CONNECTIONS   0x24
#define E_CONNECTION_ID         0x21

#define E_CONNECT_STATE_TIMEOUT         0x80
#define E_CONNECT_NOT_READY             0x81
#define E_CONNECT_TUNNELLING_TIMEOUT    0x82

//Data Link Layer  Message
//Primitive    Code
//---------------  -------

#define L_Raw_req          0x10
#define L_Data_req         0x11  //Data Service. Primitive used for transmitting a data frame
#define L_Poll_Data_req    0x13  //Poll Data Service

//FROM DATA LINK LAYER TO NETWORK LAYER
//Data Link Layer  Message
//Primitive    Code
//---------------  -------
#define L_Poll_Data_con    0x25  //Poll Data Service
#define L_Data_ind         0x29  //Data Service. Primitive used for receiving a data frame
#define L_Busmon_ind       0x2B  //Bus Monitor Service
#define L_Raw_ind          0x2D
#define L_Data_con         0x2E  //Data Service. Primitive used for local confirmation that a frame was sent(does not indicate a successful receive though)
#define L_Raw_con          0x2F

#define     SEARCH_REQUEST              0x0201
#define     SEARCH_RESPONSE             0x0202
#define     DESCRIPTION_REQUEST         0x0203
#define     DESCRIPTION_RESPONSE        0x0204
#define     CONNECTION_REQUEST          0x0205
#define     CONNECTION_RESPONSE         0x0206
#define     CONNECTIONSTATE_REQUEST     0x0207
#define     CONNECTIONSTATE_RESPONSE    0x0208
#define     DISCONNECT_REQUEST          0x0209
#define     DISCONNECT_RESPONSE         0x020a

#define     TUNNEL_REQUEST              0x0420
#define     TUNNEL_ACK                  0x0421

#define     DEVICE_CONFIGURATION_REQUEST 0x0310
#define     DEVICE_CONFIGURATION_ACK    0x0311
#define     ROUTING_INDICATION          0x0530


// NO_ERROR(0x00),
    /**
      +     * There's no connection with the given channel ID.
      +     */
// CONNECTION_ID_ERROR(0x21),
/**
      +     * There's an error with the *data* connection between client and KNXnet/IP server.
      +     */
// DATA_CONNECTION_ERROR(0x26),

/**
      +     * There's an error on the KNX bus the KNXnet/IP server is using to communicate with KNX devices.
      +     */
// KNX_CONNECTION_ERROR(0x27);

/*
Control Field 1

Bit  |
------+---------------------------------------------------------------
7   | Frame Type  - 0x0 for extended frame
|               0x1 for standard frame
------+---------------------------------------------------------------
6   | Reserved
|
------+---------------------------------------------------------------
5   | Repeat Flag - 0x0 repeat frame on medium in case of an error
|               0x1 do not repeat
------+---------------------------------------------------------------
4   | System Broadcast - 0x0 system broadcast
|                    0x1 broadcast
------+---------------------------------------------------------------
3   | Priority    - 0x0 system
|               0x1 normal
------+               0x2 urgent
2   |               0x3 low
|
------+---------------------------------------------------------------
1   | Acknowledge Request - 0x0 no ACK requested
| (L_Data.req)          0x1 ACK requested
------+---------------------------------------------------------------
0   | Confirm      - 0x0 no error
| (L_Data.con) - 0x1 error
------+---------------------------------------------------------------
*/

#define CTRL1_REPEAT    5
#define CTRL1_ACK_REQ   1
#define CTRL1_CONFIRM   0

/*
Control Field 2

Bit  |
------+---------------------------------------------------------------
7   | Destination Address Type - 0x0 individual address
|                          - 0x1 group address
------+---------------------------------------------------------------
6-4  | Hop Count (0-7)
------+---------------------------------------------------------------
3-0  | Extended Frame Format - 0x0 standard frame
------+---------------------------------------------------------------
*/

#define CTRL2_ADDR_TYPE    7

#define APCI_VALUE_READ     0x00
#define APCI_VALUE_RESP     0x40
#define APCI_VALUE_WRITE    0x80

#define INDIVIDUAL_ADDR     0x0
#define GROUP_ADDR          0x1
#define ADDR_TYPE_BIT       0x7

#endif
